<template><div><h2 id="文件存储" tabindex="-1"><a class="header-anchor" href="#文件存储"><span>文件存储</span></a></h2>
<ul>
<li><a href="#introduction">简介</a></li>
<li><a href="#configuration">配置</a>
<ul>
<li><a href="#the-local-driver">本地驱动</a></li>
<li><a href="#the-public-disk">公共磁盘</a></li>
<li><a href="#driver-prerequisites">驱动先决要求</a></li>
<li><a href="#scoped-and-read-only-filesystems">分区和只读文件系统</a></li>
<li><a href="#amazon-s3-compatible-filesystems">Amazon S3 兼容文件系统</a></li>
</ul>
</li>
<li><a href="#obtaining-disk-instances">获取磁盘实例</a>
<ul>
<li><a href="#on-demand-disks">按需配置磁盘</a></li>
</ul>
</li>
<li><a href="#retrieving-files">检索文件</a>
<ul>
<li><a href="#downloading-files">下载文件</a></li>
<li><a href="#file-urls">文件 URL</a></li>
<li><a href="#temporary-urls">临时 URL</a></li>
<li><a href="#file-metadata">文件元数据</a></li>
</ul>
</li>
<li><a href="#storing-files">保存文件</a>
<ul>
<li><a href="#prepending-appending-to-files">预置和附加文件</a></li>
<li><a href="#copying-moving-files">复制和移动文件</a></li>
<li><a href="#automatic-streaming">自动流式传输</a></li>
<li><a href="#file-uploads">文件上传</a></li>
<li><a href="#file-visibility">文件可见性</a></li>
</ul>
</li>
<li><a href="#deleting-files">删除文件</a></li>
<li><a href="#directories">目录</a></li>
<li><a href="#testing">测试</a></li>
<li><a href="#custom-filesystems">自定义文件系统</a></li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p>Laravel 提供了一个强大的文件系统抽象，这要感谢 Frank de Jonge 的 <a href="https://github.com/thephpleague/flysystem" target="_blank" rel="noopener noreferrer">Flysystem</a> PHP 包。Laravel 的 Flysystem 集成提供了 简单的驱动来处理本地文件系统、SFTP 和 Amazon S3。更棒的是，在你的本地开发机器和生产服务器之间切换这些存储选项是非常简单的，因为每个系统的 API 都是一样的。</p>
<h2 id="配置" tabindex="-1"><a class="header-anchor" href="#配置"><span>配置</span></a></h2>
<p>Laravel 的文件系统配置文件位于 <code v-pre>config/filesystems.php</code>。 在这个文件中，你可以配置你所有的文件系统「磁盘」。每个磁盘代表一个特定的存储驱动器和存储位置。 每种支持的驱动器的配置示例都包含在配置文件中, 因此你可以修改配置以反映你的存储偏好和证书。</p>
<p><code v-pre>local</code> 驱动用于与运行Laravel应用程序的服务器上存储的文件进行交互，而 <code v-pre>s3</code> 驱动用于写入 Amazon 的 S3 云存储服务。</p>
<blockquote>
<p><strong>注意</strong><br>
你可以配置任意数量的磁盘，甚至可以添加多个使用相同驱动的磁盘。</p>
</blockquote>
<h3 id="本地驱动" tabindex="-1"><a class="header-anchor" href="#本地驱动"><span>本地驱动</span></a></h3>
<p>使用 <code v-pre>local</code> 驱动时，所有文件操作都与 <code v-pre>filesystems</code> 配置文件中定义的 <code v-pre>root</code> 目录相关。 默认情况下，此值设置为 <code v-pre>storage/app</code> 目录。因此，以下方法会把文件存储在 <code v-pre>storage/app/example.txt</code>中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Storage</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">disk</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'local'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'example.txt'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Contents'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="公共磁盘" tabindex="-1"><a class="header-anchor" href="#公共磁盘"><span>公共磁盘</span></a></h3>
<p>在 <code v-pre>filesystems</code> 配置文件中定义的 <code v-pre>public</code> 磁盘适用于要公开访问的文件。默认情况下， <code v-pre>public</code> 磁盘使用 <code v-pre>local</code> 驱动，并且将这些文件存储在 <code v-pre>storage/app/public</code>目录下。</p>
<p>要使这些文件可从 web 访问，应创建从 <code v-pre>public/storage</code> 到 <code v-pre>storage/app/public</code>的符号链接。这种方式能把可公开访问文件都保留在同一个目录下，以便在使用零停机时间部署系统如 <a href="https://envoyer.io/" target="_blank" rel="noopener noreferrer">Envoyer</a> 的时候，就可以轻松地在不同的部署之间共享这些文件。</p>
<p>你可以使用 Artisan 命令 <code v-pre>storage:link</code> 来创建符号链接：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan storage:link</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>一旦一个文件被存储并且已经创建了符号链接，你就可以使用辅助函数 <code v-pre>asset</code> 来创建文件的 URL：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token function">asset</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'storage/file.txt'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你可以在 <code v-pre>filesystems</code> 配置文件中配置额外的符号链接。这些链接将会在运行 <code v-pre>storage:link</code> 命令时自动创建：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'links'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token function">public_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'storage'</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token function">storage_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app/public'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token function">public_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'images'</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token function">storage_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app/images'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="驱动先决条件" tabindex="-1"><a class="header-anchor" href="#驱动先决条件"><span>驱动先决条件</span></a></h3>
<h4 id="s3-驱动配置" tabindex="-1"><a class="header-anchor" href="#s3-驱动配置"><span>S3 驱动配置</span></a></h4>
<p>在使用 S3 驱动之前，你需要通过 Composer 安装 Flysystem S3 包：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> require league/flysystem-aws-s3-v3 <span class="token string">"^3.0"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>S3 驱动配置信息位于你的 <code v-pre>config/filesystems.php</code> 配置文件中。 此文件包含 S3 驱动的示例配置数组。 你可以使用自己的 S3 配置和凭据自由修改此数组。 为方便起见，这些环境变量与 AWS CLI 使用的命名约定相匹配。</p>
<h4 id="ftp-驱动配置" tabindex="-1"><a class="header-anchor" href="#ftp-驱动配置"><span>FTP 驱动配置</span></a></h4>
<p>在使用 FTP 驱动之前，你需要通过 Composer 安装 Flysystem FTP 包：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> require league/flysystem-ftp <span class="token string">"^3.0"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>Laravel 的 Flysystem 集成与 FTP 配合得很好； 但是，框架的默认 <code v-pre>filesystems.php</code> 配置文件中不包含示例配置。 如果需要配置 FTP 文件系统，可以使用下面的配置示例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'ftp'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'driver'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'ftp'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'host'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'FTP_HOST'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'username'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'FTP_USERNAME'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'password'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'FTP_PASSWORD'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 可选的 FTP 设置...</span></span>
<span class="line">    <span class="token comment">// 'port' => env('FTP_PORT', 21),</span></span>
<span class="line">    <span class="token comment">// 'root' => env('FTP_ROOT'),</span></span>
<span class="line">    <span class="token comment">// 'passive' => true,</span></span>
<span class="line">    <span class="token comment">// 'ssl' => true,</span></span>
<span class="line">    <span class="token comment">// 'timeout' => 30,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="sftp-驱动配置" tabindex="-1"><a class="header-anchor" href="#sftp-驱动配置"><span>SFTP 驱动配置</span></a></h4>
<p>在使用 SFTP 驱动之前，你需要通过 Composer 安装 Flysystem SFTP 包：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> require league/flysystem-sftp-v3 <span class="token string">"^3.0"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>Laravel 的 Flysystem 集成与 SFTP 配合得很好； 但是，框架的默认 <code v-pre>filesystems.php</code> 配置文件中不包含示例配置。 如果你需要配置 SFTP 文件系统，可以使用下面的配置示例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'sftp'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'driver'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'sftp'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'host'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'SFTP_HOST'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 基本身份验证设置...</span></span>
<span class="line">    <span class="token string single-quoted-string">'username'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'SFTP_USERNAME'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'password'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'SFTP_PASSWORD'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 基于SSH密钥的加密密码认证设置…</span></span>
<span class="line">    <span class="token string single-quoted-string">'privateKey'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'SFTP_PRIVATE_KEY'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'passphrase'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'SFTP_PASSPHRASE'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 可选的 SFTP 设置...</span></span>
<span class="line">    <span class="token comment">// 'hostFingerprint' => env('SFTP_HOST_FINGERPRINT'),</span></span>
<span class="line">    <span class="token comment">// 'maxTries' => 4,</span></span>
<span class="line">    <span class="token comment">// 'passphrase' => env('SFTP_PASSPHRASE'),</span></span>
<span class="line">    <span class="token comment">// 'port' => env('SFTP_PORT', 22),</span></span>
<span class="line">    <span class="token comment">// 'root' => env('SFTP_ROOT', ''),</span></span>
<span class="line">    <span class="token comment">// 'timeout' => 30,</span></span>
<span class="line">    <span class="token comment">// 'useAgent' => true,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="分区和只读文件系统" tabindex="-1"><a class="header-anchor" href="#分区和只读文件系统"><span>分区和只读文件系统</span></a></h3>
<p>分区磁盘允许你定义一个文件系统，其中所有的路径都自动带有给定的路径前缀。在创建一个分区文件系统磁盘之前，你需要通过 Composer 包管理器安装一个额外的 Flysystem 包：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> require league/flysystem-path-prefixing <span class="token string">"^3.0"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你可以通过定义一个使用 <code v-pre>scoped</code> 驱动的磁盘来创建任何现有文件系统磁盘的路径分区实例。例如，你可以创建一个磁盘，它将你现有的 <code v-pre>s3</code> 磁盘限定在特定的路径前缀上，然后使用你的分区磁盘进行的每个文件操作都将使用指定的前缀：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'s3-videos'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'driver'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'scoped'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'disk'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'s3'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'prefix'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'path/to/videos'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>「只读」磁盘允许你创建不允许写入操作的文件系统磁盘。在使用 <code v-pre>read-only</code> 配置选项之前，你需要通过 Composer 包管理器安装一个额外的 Flysystem 包：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> require league/flysystem-read-only <span class="token string">"^3.0"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>接下来，你可以在一个或多个磁盘的配置数组中包含 <code v-pre>read-only</code> 配置选项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'s3-videos'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'driver'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'s3'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line">    <span class="token string single-quoted-string">'read-only'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="amazon-s3-兼容文件系统" tabindex="-1"><a class="header-anchor" href="#amazon-s3-兼容文件系统"><span>Amazon S3 兼容文件系统</span></a></h3>
<p>默认情况下，你的应用程序的 <code v-pre>filesystems</code> 配置文件包含一个 <code v-pre>s3</code> 磁盘的磁盘配置。除了使用此磁盘与 Amazon S3 交互外，你还可以使用它与任何兼容 S3 的文件存储服务（如 <a href="https://github.com/minio/minio" target="_blank" rel="noopener noreferrer">MinIO</a> 或 <a href="https://www.digitalocean.com/products/spaces/" target="_blank" rel="noopener noreferrer">DigitalOcean Spaces</a>）进行交互。</p>
<p>通常，在更新磁盘凭据以匹配你计划使用的服务的凭据后，你只需要更新 <code v-pre>endpoint</code> 配置选项的值。此选项的值通常通过 <code v-pre>AWS_ENDPOINT</code> 环境变量定义：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'endpoint'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'AWS_ENDPOINT'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'https://minio:9000'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="minio" tabindex="-1"><a class="header-anchor" href="#minio"><span>MinIO</span></a></h4>
<p>为了让 Laravel 的 Flysystem 集成在使用 MinIO 时生成正确的 URL，你应该定义 <code v-pre>AWS_URL</code> 环境变量，使其与你的应用程序的本地 URL 匹配，并在 URL 路径中包含存储桶名称：</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line"><span class="token key attr-name">AWS_URL</span><span class="token punctuation">=</span><span class="token value attr-value">http://localhost:9000/local</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p><strong>警告</strong><br>
当使用 MinIO 时，不支持通过 <code v-pre>temporaryUrl</code> 方法生成临时存储 URL。</p>
</blockquote>
<h2 id="获取磁盘实例" tabindex="-1"><a class="header-anchor" href="#获取磁盘实例"><span>获取磁盘实例</span></a></h2>
<p><code v-pre>Storage</code> Facade 可用于与所有已配置的磁盘进行交互。例如，你可以使用 Facade 中的 <code v-pre>put</code> 方法将头像存储到默认磁盘。如果你在未先调用 <code v-pre>disk</code> 方法的情况下调用 <code v-pre>Storage</code> Facade 中的方法，则该方法将自动传递给默认磁盘：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Storage</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'avatars/1'</span><span class="token punctuation">,</span> <span class="token variable">$content</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你的应用与多个磁盘进行交互，可使用 <code v-pre>Storage</code> Facade 中的 <code v-pre>disk</code> 方法对特定磁盘上的文件进行操作：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">disk</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'s3'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'avatars/1'</span><span class="token punctuation">,</span> <span class="token variable">$content</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="按需配置磁盘" tabindex="-1"><a class="header-anchor" href="#按需配置磁盘"><span>按需配置磁盘</span></a></h3>
<p>有时你可能希望在运行时使用给定配置创建磁盘，而无需在应用程序的 <code v-pre>filesystems</code> 配置文件中实际存在该配置。为了实现这一点，你可以将配置数组传递给 <code v-pre>Storage</code> Facade 的 <code v-pre>build</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Storage</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$disk</span> <span class="token operator">=</span> <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">build</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'driver'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'local'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'root'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'/path/to/root'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$disk</span><span class="token operator">-></span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'image.jpg'</span><span class="token punctuation">,</span> <span class="token variable">$content</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="检索文件" tabindex="-1"><a class="header-anchor" href="#检索文件"><span>检索文件</span></a></h2>
<p><code v-pre>get</code> 方法可用于检索文件的内容。该方法将返回文件的原始字符串内容。切记，所有文件路径的指定都应该相对于该磁盘所配置的「root」目录：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$contents</span> <span class="token operator">=</span> <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'file.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><code v-pre>exists</code> 方法可以用来判断一个文件是否存在于磁盘上：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">disk</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'s3'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">exists</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'file.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>missing</code> 方法可以用来判断一个文件是否缺失于磁盘上：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">disk</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'s3'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">missing</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'file.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="下载文件" tabindex="-1"><a class="header-anchor" href="#下载文件"><span>下载文件</span></a></h3>
<p><code v-pre>download</code> 方法可以用来生成一个响应，强制用户的浏览器下载给定路径的文件。<code v-pre>download</code> 方法接受一个文件名作为方法的第二个参数，这将决定用户下载文件时看到的文件名。最后，你可以传递一个 HTTP 头部的数组作为方法的第三个参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">download</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'file.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">download</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'file.jpg'</span><span class="token punctuation">,</span> <span class="token variable">$name</span><span class="token punctuation">,</span> <span class="token variable">$headers</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="文件-url" tabindex="-1"><a class="header-anchor" href="#文件-url"><span>文件 URL</span></a></h3>
<p>你可以使用 <code v-pre>url</code> 方法来获取给定文件的 URL。如果你使用的是<code v-pre>local</code> 驱动，这通常只会在给定路径前加上 <code v-pre>/storage</code>，并返回一个相对 URL 到文件。如果你使用的是 <code v-pre>s3</code> 驱动，将返回完全限定的远程 URL：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Storage</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">url</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'file.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当使用 <code v-pre>local</code> 驱动时，所有应该公开访问的文件都应放置在 <code v-pre>storage/app/public</code> 目录中。此外，你应该在 <code v-pre>public/storage</code> 处 <a href="#the-public-disk">创建一个符号连接</a> 指向 <code v-pre>storage/app/public</code> 目录。</p>
<blockquote>
<p><strong>警告</strong><br>
当使用 <code v-pre>local</code> 驱动时，url 的返回值不是 URL 编码的。因此，我们建议始终使用能够创建有效 URL 的名称存储文件。</p>
</blockquote>
<h4 id="定制-url-的-host" tabindex="-1"><a class="header-anchor" href="#定制-url-的-host"><span>定制 URL 的 Host</span></a></h4>
<p>如果你想预定义使用 <code v-pre>Storage</code> Facade 生成的 URL 的 Host，则可以在磁盘的配置数组中添加一个 <code v-pre>url</code> 选项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'public'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'driver'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'local'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'root'</span> <span class="token operator">=></span> <span class="token function">storage_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app/public'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'url'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'APP_URL'</span><span class="token punctuation">)</span><span class="token operator">.</span><span class="token string single-quoted-string">'/storage'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'visibility'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'public'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="临时-url" tabindex="-1"><a class="header-anchor" href="#临时-url"><span>临时 URL</span></a></h3>
<p>使用 <code v-pre>temporaryUrl</code> 方法，你可以为使用 <code v-pre>s3</code> 驱动存储的文件创建临时 URL。此方法接受一个路径和一个 <code v-pre>DateTime</code> 实例，指定 URL 的过期时间：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Storage</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">temporaryUrl</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token string single-quoted-string">'file.jpg'</span><span class="token punctuation">,</span> <span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">addMinutes</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你需要指定额外的 <a href="https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectGET.html#RESTObjectGET-requests" target="_blank" rel="noopener noreferrer">S3 请求参数</a>，你可以将请求参数数组作为第三个参数传递给<code v-pre>temporaryUrl</code> 方法。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">temporaryUrl</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token string single-quoted-string">'file.jpg'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">addMinutes</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'ResponseContentType'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'application/octet-stream'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'ResponseContentDisposition'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'attachment; filename=file2.jpg'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你需要为一个特定的存储磁盘定制临时 URL 的创建方式，可以使用 <code v-pre>buildTemporaryUrlsUsing</code> 方法。例如，如果你有一个控制器允许你通过不支持临时 URL 的磁盘下载存储的文件，这可能很有用。通常，此方法应从服务提供者的 <code v-pre>boot</code> 方法中调用：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Providers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">DateTime</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Storage</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>URL</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>ServiceProvider</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">AppServiceProvider</span> <span class="token keyword">extends</span> <span class="token class-name">ServiceProvider</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 启动任何应用程序服务。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">disk</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'local'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">buildTemporaryUrlsUsing</span><span class="token punctuation">(</span></span>
<span class="line">            <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$path</span><span class="token punctuation">,</span> <span class="token class-name type-declaration">DateTime</span> <span class="token variable">$expiration</span><span class="token punctuation">,</span> <span class="token keyword type-hint">array</span> <span class="token variable">$options</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                <span class="token keyword">return</span> <span class="token class-name static-context">URL</span><span class="token operator">::</span><span class="token function">temporarySignedRoute</span><span class="token punctuation">(</span></span>
<span class="line">                    <span class="token string single-quoted-string">'files.download'</span><span class="token punctuation">,</span></span>
<span class="line">                    <span class="token variable">$expiration</span><span class="token punctuation">,</span></span>
<span class="line">                    <span class="token function">array_merge</span><span class="token punctuation">(</span><span class="token variable">$options</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'path'</span> <span class="token operator">=></span> <span class="token variable">$path</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">            <span class="token punctuation">}</span></span>
<span class="line">        <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="临时上传-url" tabindex="-1"><a class="header-anchor" href="#临时上传-url"><span>临时上传 URL</span></a></h4>
<blockquote>
<p><strong>警告</strong><br>
生成临时上传 URL 的能力仅由 <code v-pre>s3</code> 驱动支持。</p>
</blockquote>
<p>如果你需要生成一个临时 URL，可以直接从客户端应用程序上传文件，你可以使用 <code v-pre>temporaryUploadUrl</code> 方法。此方法接受一个路径和一个 <code v-pre>DateTime</code> 实例，指定 URL 应该在何时过期。<code v-pre>temporaryUploadUrl</code> 方法返回一个关联数组，可以解构为上传 URL 和应该包含在上传请求中的头部：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Storage</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token punctuation">[</span><span class="token string single-quoted-string">'url'</span> <span class="token operator">=></span> <span class="token variable">$url</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'headers'</span> <span class="token operator">=></span> <span class="token variable">$headers</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">temporaryUploadUrl</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token string single-quoted-string">'file.jpg'</span><span class="token punctuation">,</span> <span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">addMinutes</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>此方法主要用于无服务器环境，需要客户端应用程序直接将文件上传到云存储系统（如 Amazon S3）。</p>
<h3 id="文件元数据" tabindex="-1"><a class="header-anchor" href="#文件元数据"><span>文件元数据</span></a></h3>
<p>除了读写文件，Laravel 还可以提供有关文件本身的信息。例如，<code v-pre>size</code> 方法可用于获取文件大小（以字节为单位）：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Storage</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$size</span> <span class="token operator">=</span> <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'file.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>lastModified</code> 方法返回上次修改文件时的时间戳：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$time</span> <span class="token operator">=</span> <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">lastModified</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'file.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>可以通过 <code v-pre>mimeType</code> 方法获取给定文件的 MIME 类型：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$mime</span> <span class="token operator">=</span> <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">mimeType</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'file.jpg'</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="文件路径" tabindex="-1"><a class="header-anchor" href="#文件路径"><span>文件路径</span></a></h4>
<p>你可以使用 <code v-pre>path</code> 方法获取给定文件的路径。如果你使用的是 <code v-pre>local</code> 驱动，这将返回文件的绝对路径。如果你使用的是 <code v-pre>s3</code> 驱动，此方法将返回 S3 存储桶中文件的相对路径：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Storage</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'file.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="保存文件" tabindex="-1"><a class="header-anchor" href="#保存文件"><span>保存文件</span></a></h2>
<p>可以使用 <code v-pre>put</code> 方法将文件内容存储在磁盘上。你还可以将 PHP <code v-pre>resource</code> 传递给 <code v-pre>put</code> 方法，该方法将使用 Flysystem 的底层流支持。请记住，应相对于为磁盘配置的「根」目录指定所有文件路径：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Storage</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'file.jpg'</span><span class="token punctuation">,</span> <span class="token variable">$contents</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'file.jpg'</span><span class="token punctuation">,</span> <span class="token variable">$resource</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="写入失败" tabindex="-1"><a class="header-anchor" href="#写入失败"><span>写入失败</span></a></h4>
<p>如果 <code v-pre>put</code> 方法（或其他「写入」操作）无法将文件写入磁盘，将返回 <code v-pre>false</code>。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span> <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'file.jpg'</span><span class="token punctuation">,</span> <span class="token variable">$contents</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// 该文件无法写入磁盘...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以在你的文件系统磁盘的配置数组中定义 <code v-pre>throw</code> 选项。当这个选项被定义为 <code v-pre>true</code> 时，「写入」的方法如 <code v-pre>put</code> 将在写入操作失败时抛出一个 <code v-pre>League\Flysystem\UnableToWriteFile</code> 的实例。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'public'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'driver'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'local'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line">    <span class="token string single-quoted-string">'throw'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="追加内容到文件开头或结尾" tabindex="-1"><a class="header-anchor" href="#追加内容到文件开头或结尾"><span>追加内容到文件开头或结尾</span></a></h3>
<p><code v-pre>prepend</code> 和 <code v-pre>append</code> 方法允许你将内容写入文件的开头或结尾：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">prepend</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'file.log'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Prepended Text'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">append</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'file.log'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Appended Text'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="复制-移动文件" tabindex="-1"><a class="header-anchor" href="#复制-移动文件"><span>复制 / 移动文件</span></a></h3>
<p><code v-pre>copy</code> 方法可用于将现有文件复制到磁盘上的新位置，而 <code v-pre>move</code> 方法可用于重命名现有文件或将其移动到新位置：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">copy</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'old/file.jpg'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'new/file.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">move</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'old/file.jpg'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'new/file.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="自动流式传输" tabindex="-1"><a class="header-anchor" href="#自动流式传输"><span>自动流式传输</span></a></h3>
<p>将文件流式传输到存储位置可显著减少内存使用量。如果你希望 Laravel 自动管理将给定文件流式传输到你的存储位置，你可以使用 <code v-pre>putFile</code> 或 <code v-pre>putFileAs</code> 方法。此方法接受一个 <code v-pre>Illuminate\Http\File</code> 或 <code v-pre>Illuminate\Http\UploadedFile</code> 实例，并自动将文件流式传输到你所需的位置：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>File</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Storage</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 为文件名自动生成一个唯一的 ID...</span></span>
<span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">putFile</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'photos'</span><span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/path/to/photo'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 手动指定一个文件名...</span></span>
<span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">putFileAs</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'photos'</span><span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/path/to/photo'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'photo.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>关于 putFile 方法有几点重要的注意事项。注意，我们只指定了目录名称而不是文件名。默认情况下，<code v-pre>putFile</code> 方法将生成一个唯一的 ID 作为文件名。文件的扩展名将通过检查文件的 MIME 类型来确定。文件的路径将由 <code v-pre>putFile</code> 方法返回，因此你可以将路径（包括生成的文件名）存储在数据库中。</p>
<p><code v-pre>putFile</code> 和 <code v-pre>putFileAs</code> 方法还接受一个参数来指定存储文件的「可见性」。如果你将文件存储在云盘（如 Amazon S3）上，并希望文件通过生成的 URL 公开访问，这一点特别有用：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">putFile</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'photos'</span><span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/path/to/photo'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'public'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="文件上传" tabindex="-1"><a class="header-anchor" href="#文件上传"><span>文件上传</span></a></h3>
<p>在网络应用程序中，存储文件的最常见用例之一是存储用户上传的文件，如照片和文档。Laravel 使用上传文件实例上的 <code v-pre>store</code> 方法非常容易地存储上传的文件。使用你希望存储上传文件的路径调用 <code v-pre>store</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers<span class="token punctuation">\</span>Controller</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">UserAvatarController</span> <span class="token keyword">extends</span> <span class="token class-name">Controller</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 更新用户的头像。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">update</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">string</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$path</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">file</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'avatar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">store</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'avatars'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$path</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>关于这个例子有几点重要的注意事项。注意，我们只指定了目录名称而不是文件名。默认情况下，<code v-pre>store</code> 方法将生成一个唯一的 ID 作为文件名。文件的扩展名将通过检查文件的 MIME 类型来确定。文件的路径将由 <code v-pre>store</code> 方法返回，因此你可以将路径（包括生成的文件名）存储在数据库中。</p>
<p>你也可以在 <code v-pre>Storage</code> Facade 上调用 <code v-pre>putFile</code> 方法来执行与上面示例相同的文件存储操作：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">putFile</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'avatars'</span><span class="token punctuation">,</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">file</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'avatar'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="指定一个文件名" tabindex="-1"><a class="header-anchor" href="#指定一个文件名"><span>指定一个文件名</span></a></h4>
<p>如果你不希望文件名被自动分配给你存储的文件，你可以使用 <code v-pre>storeAs</code> 方法，该方法接收路径、文件名和（可选的）磁盘作为其参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">file</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'avatar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">storeAs</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token string single-quoted-string">'avatars'</span><span class="token punctuation">,</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token property">id</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以在 <code v-pre>Storage</code> Facade 使用 <code v-pre>putFileAs</code> 方法，它将执行与上面示例相同的文件存储操作：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">putFileAs</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token string single-quoted-string">'avatars'</span><span class="token punctuation">,</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">file</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'avatar'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token property">id</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>警告</strong><br>
不可打印和无效的 Unicode 字符将自动从文件路径中删除。因此，你可能希望在将文件路径传递给 Laravel 的文件存储方法之前对其进行清理。文件路径使用 <code v-pre>League\Flysystem\WhitespacePathNormalizer::normalizePath</code> 方法进行规范化。</p>
</blockquote>
<h4 id="指定一个磁盘" tabindex="-1"><a class="header-anchor" href="#指定一个磁盘"><span>指定一个磁盘</span></a></h4>
<p>默认情况下，此上传文件的 <code v-pre>store</code> 方法将使用你的默认磁盘。如果你想指定另一个磁盘，将磁盘名称作为第二个参数传递给 <code v-pre>store</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">file</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'avatar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">store</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token string single-quoted-string">'avatars/'</span><span class="token operator">.</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'s3'</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你正在使用 <code v-pre>storeAs</code> 方法，你可以将磁盘名称作为第三个参数传递给该方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">file</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'avatar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">storeAs</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token string single-quoted-string">'avatars'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'s3'</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="其他上传文件的信息" tabindex="-1"><a class="header-anchor" href="#其他上传文件的信息"><span>其他上传文件的信息</span></a></h4>
<p>如果您想获取上传文件的原始名称和扩展名，可以使用 <code v-pre>getClientOriginalName</code> 和 <code v-pre>getClientOriginalExtension</code> 方法来实现：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$file</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">file</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'avatar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$name</span> <span class="token operator">=</span> <span class="token variable">$file</span><span class="token operator">-></span><span class="token function">getClientOriginalName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$extension</span> <span class="token operator">=</span> <span class="token variable">$file</span><span class="token operator">-></span><span class="token function">getClientOriginalExtension</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>然而，请记住，<code v-pre>getClientOriginalName</code> 和 <code v-pre>getClientOriginalExtension</code> 方法被认为是不安全的，因为文件名和扩展名可能被恶意用户篡改。因此，你通常应该更喜欢使用 <code v-pre>hashName</code> 和 <code v-pre>extension</code> 方法来获取给定文件上传的名称和扩展名：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$file</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">file</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'avatar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$name</span> <span class="token operator">=</span> <span class="token variable">$file</span><span class="token operator">-></span><span class="token function">hashName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 生成一个唯一的、随机的名字...</span></span>
<span class="line"><span class="token variable">$extension</span> <span class="token operator">=</span> <span class="token variable">$file</span><span class="token operator">-></span><span class="token function">extension</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 根据文件的 MIME 类型来确定文件的扩展名...</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="文件可见性" tabindex="-1"><a class="header-anchor" href="#文件可见性"><span>文件可见性</span></a></h3>
<p>在 Laravel 的 Flysystem 集成中，「visibility」 是跨多个平台的文件权限的抽象。文件可以被声明为 <code v-pre>public</code> 或 <code v-pre>private</code>。当一个文件被声明为 <code v-pre>public</code> 时，你表示该文件通常应该被其他人访问。例如，在使用 S3 驱动程序时，你可以检索 <code v-pre>public</code> 文件的 URL。</p>
<p>你可以通过 <code v-pre>put</code> 方法在写入文件时设置可见性：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Storage</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'file.jpg'</span><span class="token punctuation">,</span> <span class="token variable">$contents</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'public'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果文件已经被存储，可以通过 <code v-pre>getVisibility</code> 和 <code v-pre>setVisibility</code> 方法检索和设置其可见性：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$visibility</span> <span class="token operator">=</span> <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">getVisibility</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'file.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">setVisibility</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'file.jpg'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'public'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在与上传文件交互时，你可以使用 <code v-pre>storePublicly</code> 和 <code v-pre>storePubliclyAs</code> 方法将上传文件存储为 <code v-pre>public</code> 可见性</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">file</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'avatar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">storePublicly</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'avatars'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'s3'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">file</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'avatar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">storePubliclyAs</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token string single-quoted-string">'avatars'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'s3'</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="本地文件和可见性" tabindex="-1"><a class="header-anchor" href="#本地文件和可见性"><span>本地文件和可见性</span></a></h4>
<p>当使用 <code v-pre>local</code> 驱动时，<code v-pre>public</code><a href="#file-visibility">可见性</a>转换为目录的 <code v-pre>0755</code> 权限和文件的 <code v-pre>0644</code> 权限。你可以在你的应用程序的 <code v-pre>filesystems</code> 配置文件中修改权限映射：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'local'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'driver'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'local'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'root'</span> <span class="token operator">=></span> <span class="token function">storage_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'permissions'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'file'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'public'</span> <span class="token operator">=></span> <span class="token number">0644</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'private'</span> <span class="token operator">=></span> <span class="token number">0600</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'dir'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'public'</span> <span class="token operator">=></span> <span class="token number">0755</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'private'</span> <span class="token operator">=></span> <span class="token number">0700</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="删除文件" tabindex="-1"><a class="header-anchor" href="#删除文件"><span>删除文件</span></a></h2>
<p><code v-pre>delete</code> 方法接收一个文件名或一个文件名数组来将其从磁盘中删除：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Storage</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'file.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'file.jpg'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'file2.jpg'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果需要，你可以指定应从哪个磁盘删除文件。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Storage</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">disk</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'s3'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'path/file.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="目录" tabindex="-1"><a class="header-anchor" href="#目录"><span>目录</span></a></h2>
<h4 id="获取目录下所有的文件" tabindex="-1"><a class="header-anchor" href="#获取目录下所有的文件"><span>获取目录下所有的文件</span></a></h4>
<p><code v-pre>files</code> 将以数组的形式返回给定目录下所有的文件。如果你想要检索给定目录的所有文件及其子目录的所有文件，你可以使用 <code v-pre>allFiles</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Storage</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$files</span> <span class="token operator">=</span> <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">files</span><span class="token punctuation">(</span><span class="token variable">$directory</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$files</span> <span class="token operator">=</span> <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">allFiles</span><span class="token punctuation">(</span><span class="token variable">$directory</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="获取特定目录下的子目录" tabindex="-1"><a class="header-anchor" href="#获取特定目录下的子目录"><span>获取特定目录下的子目录</span></a></h4>
<p><code v-pre>directories</code> 方法以数组的形式返回给定目录中的所有目录。此外，你还可以使用 <code v-pre>allDirectories</code> 方法递归地获取给定目录中的所有目录及其子目录中的目录：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$directories</span> <span class="token operator">=</span> <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">directories</span><span class="token punctuation">(</span><span class="token variable">$directory</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$directories</span> <span class="token operator">=</span> <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">allDirectories</span><span class="token punctuation">(</span><span class="token variable">$directory</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="创建目录" tabindex="-1"><a class="header-anchor" href="#创建目录"><span>创建目录</span></a></h4>
<p><code v-pre>makeDirectory</code> 方法可递归的创建指定的目录：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">makeDirectory</span><span class="token punctuation">(</span><span class="token variable">$directory</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="删除一个目录" tabindex="-1"><a class="header-anchor" href="#删除一个目录"><span>删除一个目录</span></a></h4>
<p>最后，<code v-pre>deleteDirectory</code> 方法可用于删除一个目录及其下所有的文件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">deleteDirectory</span><span class="token punctuation">(</span><span class="token variable">$directory</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="测试" tabindex="-1"><a class="header-anchor" href="#测试"><span>测试</span></a></h2>
<p><code v-pre>Storage</code> 门面类的 <code v-pre>fake</code> 方法可以轻松创建一个虚拟磁盘，与<code v-pre>Illuminate\Http\UploadedFile</code> 类配合使用，大大简化了文件的上传测试。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">Tests<span class="token punctuation">\</span>Feature</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>UploadedFile</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Storage</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Tests<span class="token punctuation">\</span>TestCase</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">ExampleTest</span> <span class="token keyword">extends</span> <span class="token class-name">TestCase</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">test_albums_can_be_uploaded</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">fake</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'photos'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token variable">$response</span> <span class="token operator">=</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">json</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'POST'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'/photos'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token class-name static-context">UploadedFile</span><span class="token operator">::</span><span class="token function">fake</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">image</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'photo1.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token class-name static-context">UploadedFile</span><span class="token operator">::</span><span class="token function">fake</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">image</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'photo2.jpg'</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 断言存储了一个或多个文件。</span></span>
<span class="line">        <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">disk</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'photos'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">assertExists</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'photo1.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">disk</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'photos'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">assertExists</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'photo1.jpg'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'photo2.jpg'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 断言一个或多个文件未存储。</span></span>
<span class="line">        <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">disk</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'photos'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">assertMissing</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'missing.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">disk</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'photos'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">assertMissing</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'missing.jpg'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'non-existing.jpg'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 断言给定目录为空。</span></span>
<span class="line">        <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">disk</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'photos'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">assertDirectoryEmpty</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/wallpapers'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>默认情况下，<code v-pre>fake</code> 方法将删除临时目录中的所有文件。如果你想保留这些文件，你可以使用 “persistentFake” 方法代替。有关测试文件上传的更多信息，您可以查阅 <a href="https://learnku.com/docs/laravel/10.x/http-tests#testing-file-uploads" target="_blank" rel="noopener noreferrer">HTTP 测试文档的文件上传</a>.</p>
<blockquote>
<p><strong>警告</strong><br>
<code v-pre>image</code> 方法需要 <a href="https://www.php.net/manual/en/book.image.php" target="_blank" rel="noopener noreferrer">GD 扩展</a> .</p>
</blockquote>
<h2 id="自定义文件系统" tabindex="-1"><a class="header-anchor" href="#自定义文件系统"><span>自定义文件系统</span></a></h2>
<p>Laravel 内置的文件系统提供了一些开箱即用的驱动；当然，它不仅仅是这些，它还提供了与其他存储系统的适配器。通过这些适配器，你可以在你的 Laravel 应用中创建自定义驱动。</p>
<p>要安装自定义文件系统，你可能需要一个文件系统适配器。让我们将社区维护的 Dropbox 适配器添加到项目中：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> require spatie/flysystem-dropbox</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>接下来，你可以在 <a href="https://learnku.com/docs/laravel/10.x/providers" target="_blank" rel="noopener noreferrer">服务提供者</a> 中注册一个带有 <code v-pre>boot</code> 方法的驱动。在提供者的 <code v-pre>boot</code> 方法中，你可以使用 <code v-pre>Storage</code> 门面的 <code v-pre>extend</code> 方法来定义一个自定义驱动：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Providers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Application</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Filesystem<span class="token punctuation">\</span>FilesystemAdapter</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Storage</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>ServiceProvider</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">League<span class="token punctuation">\</span>Flysystem<span class="token punctuation">\</span>Filesystem</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Spatie<span class="token punctuation">\</span>Dropbox<span class="token punctuation">\</span>Client</span> <span class="token keyword">as</span> DropboxClient<span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Spatie<span class="token punctuation">\</span>FlysystemDropbox<span class="token punctuation">\</span>DropboxAdapter</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">AppServiceProvider</span> <span class="token keyword">extends</span> <span class="token class-name">ServiceProvider</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 注册任意应用程序服务。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">register</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">// ...</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 引导任何应用程序服务。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token class-name static-context">Storage</span><span class="token operator">::</span><span class="token function">extend</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'dropbox'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Application</span> <span class="token variable">$app</span><span class="token punctuation">,</span> <span class="token keyword type-hint">array</span> <span class="token variable">$config</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token variable">$adapter</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">DropboxAdapter</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">DropboxClient</span><span class="token punctuation">(</span></span>
<span class="line">                <span class="token variable">$config</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'authorization_token'</span><span class="token punctuation">]</span></span>
<span class="line">            <span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">            <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">FilesystemAdapter</span><span class="token punctuation">(</span></span>
<span class="line">                <span class="token keyword">new</span> <span class="token class-name">Filesystem</span><span class="token punctuation">(</span><span class="token variable">$adapter</span><span class="token punctuation">,</span> <span class="token variable">$config</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token variable">$adapter</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token variable">$config</span></span>
<span class="line">            <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>extend</code> 方法的第一个参数是驱动程序的名称，第二个参数是接收 <code v-pre>$app</code> 和 <code v-pre>$config</code> 变量的闭包。闭包必须返回的实例 <code v-pre>League\Flysystem\Filesystem</code>。<code v-pre>$config</code> 变量包含 <code v-pre>config/filesystems.php</code> 为指定磁盘定义的值。</p>
<p>一旦创建并注册了扩展的服务提供商，就可以 <code v-pre>dropbox</code> 在 <code v-pre>config/filesystems.php</code> 配置文件中使用该驱动程序。</p>
<blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/10.x/filesystem/14865" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/10.x/fi...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/10.x/filesystem/14865" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/10.x/fi...</a></p>
</blockquote>
</div></template>


